pub fn atoi(s: &str) -> i32 {
    let mut result = 0;
    let mut sign = 1;

    // Trim whitespace from the beginning
    let mut s = s.trim_start();

    // Handle optional sign
    if let Some(first_char) = s.chars().next() {
        if first_char == '+' {
            s = &s[1..];
        } else if first_char == '-' {
            sign = -1;
            s = &s[1..];
        }
    }

    // Iterate through the string and convert characters to digits
    for c in s.chars() {
        if let Some(digit) = c.to_digit(10) {
            // Check for overflow
            if result > i32::MAX as u32 / 10 || (result == i32::MAX as u32 / 10 && digit > 7) {
                return if sign == 1 { i32::MAX } else { i32::MIN };
            }
            result = result * 10 + digit as u32;
        } else {
            break; // Stop at the first non-digit character
        }
    }

    sign * result as i32
}
